<?php

// +----------------------------------------------------------------------

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Module as ModuleModel;
use app\merch\model\Merch as MerchModel;
use app\merch\model\Child as ChildModel;
use app\merch\model\Order as OrderModel;
use app\merch\model\Qrcode as QrcodeModel;
use wxpay\PayVthree;
use think\facade\Db;
/**
 * 商户订单Api控制器
 * @package app\api\controller
 */
class Order extends Api
{

	//设置活动金额
	public function merch()
	{
		if (request()->isPost()) {
			// 获取数据
			$data = request()->post();
			if(!isset($data['code'])) $this->error('参数错误');
			$code = explode('?', trim($data['code']));
			$qrcode = QrcodeModel::getInfo($code[0]);
			if (!$qrcode || $qrcode['sub_id'] == 0){
				// trace('getmerch:'.json_encode($data['code']), 'error');
				$this->error('该收款码已禁用或未绑定商户');
			} 
			$info = ChildModel::getInfo($qrcode['sub_id']);
			if (!$info) $this->error('商户终端不可用');
			$return = [
				'id'	=> $info['id'],
				'ad'	=> $info['ad'],
				'logo'	=> $info['logo'],
				'code'	=> $info['code'],
				'merch'	=> $info['merch'],
				'name'	=> $info['name'],
				'tip'	=> $info['tip'],
				'tips'	=> $info['tips'],
			];
			$this->success('请求成功', $return);
		}else{
			$this->success('连接成功');
		}
	}
	//创建订单
	public function create()
	{
		if (request()->isPost()){
			if (empty($this->token)) $this->error(['code' => 10001, 'msg' => '登录已失效!']);
			$auth = cache($this->token);
			if (!$auth) $this->error(['code' => 10001, 'msg' => '登录已失效!']);
			$info = request()->post();
			if (!isset($info['total'])) $this->error('金额信息错误');
			if (!isset($info['child'])) $this->error('终端信息错误');
			$order = new OrderModel;
			$pay = $order->createOrder($info['total'], $auth['openid'], $info['child'], 'wechat', 2);
			if (!empty($pay['out_trade_no'])){
				unset($pay['out_trade_no']);
			} 
			if (false === $pay) $this->error($order->getError());
			$this->success('获取成功', $pay);
		}
		$this->success('接口请求成功');
	}
	//创建老小程序订单
	public function createOld()
	{
		if (request()->isPost()){
			$appid = $this->request->header('Zz-Wxapp-AppId');
			if (empty($appid) || $appid != config('wechat.wxapp.appid_old')) $this->error('非法请求');
			$info = request()->post();
			if (!isset($info['total'])) $this->error('金额信息错误');
			if (!isset($info['child'])) $this->error('终端信息错误');
			if (!isset($info['openid'])) $this->error(['code' => 10001, 'msg' => '登录已失效!']);
			$order = new OrderModel;
			$pay = $order->createOrder($info['total'], $info['openid'], $info['child'], 'wechat', 4);
			if (false === $pay) $this->error($order->getError());
			$this->success('获取成功', $pay);
		}
		$this->success('接口请求成功');
	}
	//查询订单
	public function list()
	{
		if (request()->isPost()){
			if (empty($this->token)) $this->error(['code' => 10001, 'msg' => '登录已失效!']);
			$post = request()->post();
			if (!isset($post['type']) || !isset($post['page']) || !isset($post['page_size'])) $this->error('参数错误');
			$auth = cache($this->token);
			if (!$auth) $this->error(['code' => 10001, 'msg' => '登录已失效!']);
			// 设置存档表后缀
			$suffix = '_'.date('Ym');
			$list_month = OrderModel::getMonth();
			// 检查是否存在存档表
			if (!isset($list_month[$suffix])) $suffix = '';
			$map = [
				['payer', '=', $auth['openid']],
			];
			if ($post['type'] == 1) {
				$map[] = ['status', '=', 1];
			} else {
				$map[] = ['status', '=', 3];
			}
			$list = OrderModel::suffix($suffix)->where($map)->page($post['page'], $post['page_size'])->select()->toArray();
			if ($list) $this->success('获取成功', $list);
			$this->error('暂无数据');
		}
		$this->success('接口请求成功');
	}
}
